package com.community.manage.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列创建
 */
@Configuration
public class RabbitMqDeadConfig {


    @Bean
    public Queue createGroupDeadQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Map<String, Object> hashMap = new HashMap<>();
        // 队列过期时间
        hashMap.put("x-message-ttl",86400000);
        // 转接的交换机
        hashMap.put("x-dead-letter-exchange","group-direct");
        // 转接队列的rounting-key
        hashMap.put("x-dead-letter-routing-key","group.release.queue");
        return new Queue("group-dead-queue",true,false,false,hashMap);
    }

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue createGroupReleaseQueue(){
        return new Queue("group-release-queue");
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public Exchange createGroupExchange() {
        DirectExchange directExchange = new DirectExchange("group-direct");
        return directExchange;
    }

    /**
     * 绑定
     * @return
     */
    @Bean
    public Binding createReleaseBinding() {
        return new Binding("group-release-queue",Binding.DestinationType.QUEUE,
                "group-direct","group.release.queue",null);
    }

    /**
     * 绑定
     * @return
     */
    @Bean
    public Binding createDeadBinding() {
        return new Binding("group-dead-queue",Binding.DestinationType.QUEUE,
                "group-direct","group.dead.queue",null);
    }

}
